package com.jackeys.piaems.controller;

import com.jackeys.piaems.annotation.PassJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: WebSocketServer
 * @Description: websocket服务器
 * @Author: Jackeys
 * @Date: 2021-06-29 08:56
 * @Version: 1.0
 */
@Slf4j
@ServerEndpoint("/ws")
@Component
public class WebSocketServer {
    @PostConstruct
    public void init() {
        log.info("实时刷新服务器启动成功");
    }

    private final static AtomicInteger onlineCount = new AtomicInteger(0);
    private static CopyOnWriteArraySet<Session> sessionSet = new CopyOnWriteArraySet<Session>();

    /**
     * @Name: onOpen
     * @Description: 连接后调用
     * @Param: [session]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 8:58
     * @Version: 1.0
     */
    @PassJwt
    @OnOpen
    public void onOpen(Session session) {
        sessionSet.add(session);
        int cnt = onlineCount.incrementAndGet();
        log.debug("第" + cnt + "连接成功");
    }


    /**
     * @Name: onClose
     * @Description: 关闭后调用
     * @Param: [session]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 8:59
     * @Version: 1.0
     */
    @PassJwt
    @OnClose
    public void onClose(Session session) {
        sessionSet.remove(session);
        onlineCount.decrementAndGet();
        log.debug("实时刷新关闭一个连接");
    }

    /**
     * @Name: onError
     * @Description: 出错后调用
     * @Param: [session, e]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 8:59
     * @Version: 1.0
     */
    @PassJwt
    @OnError
    public void onError(Session session, Throwable e) {
        log.debug(session.getId() + "连接出错了。。");
    }

    /**
     * @Name: sendMession
     * @Description: 收到消息后调用
     * @Param: [session, message]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 8:59
     * @Version: 1.0
     */
    @PassJwt
    @OnMessage
    public void sendMession(Session session, String message) {
        sendMessage(session, "收到消息，消息内容：" + message);
    }

    /**
     * @Name: sendMessage
     * @Description: 发送消息
     * @Param: [session, message]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 8:59
     * @Version: 1.0
     */
    @PassJwt
    public static void sendMessage(Session session, String message) {
        try {
            session.getBasicRemote().sendText(String.format("%s (From Server，Session ID=%s)", message, session.getId()));
        } catch (Exception e) {

        }
    }


    /**
     * @Name: broadCastInfo
     * @Description: 群发消息
     * @Param: [message]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 9:00
     * @Version: 1.0
     */
    public static void broadCastInfo(String message) throws IOException {
        for (Session session : sessionSet) {
            if (session.isOpen()) {
                sendMessage(session, message);
            }
        }
    }

    /**
     * @Name: sendMessage
     * @Description: 发送给指定session
     * @Param: [message, sessionId]
     * @return: void
     * @Author: Jackeys
     * @Date: 2021/6/29 9:00
     * @Version: 1.0
     */
    public static void sendMessage(String message, String sessionId) throws IOException {
        Session session = null;
        for (Session s : sessionSet) {
            if (s.getId().equals(sessionId)) {
                session = s;
                break;
            }
        }
        if (session != null) {
            sendMessage(session, message);
        } else {
            System.out.println("没有找到你指定ID的会话：{}" + sessionId);
        }
    }
}